package cn.com.open.itembank.controller;

import cn.com.open.itembank.dto.*;
import cn.com.open.itembank.entity.QuestionType;
import cn.com.open.itembank.service.ItemBankService;
import cn.com.open.itembank.entity.CourseItemBank;
import cn.com.open.itembank.service.QuestionTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by lucky on 2017/9/4.
 */
@RestController
@RequestMapping(value = "/itemBank")
public class ItemBankController {

    /**
     * The Service.
     */
    @Autowired
    ItemBankService service;

    /**
     * The Question type service.
     */
    @Autowired
    QuestionTypeService questionTypeService;

    /**
     * 判断给定appkey是否包含在配置文件的applist许可列表内，如果包含则说明拥有删除题库权的权利.
     *
     * @param appKey appKey
     * @return true ，拥有；false，不拥有
     */
    @GetMapping(value = "/hasSuperPermission")
    public boolean hasSuperPermission(@RequestParam("appId") String appKey) {
        return service.hasSuperPermission(appKey);
    }

    /**
     * 判断给定的appkey是否拥有给定的题库id的管理权限，如果是题库所有者或者拥有超级权限均视为拥有权限.
     *
     * @param itemBankId 题库id
     * @param appKey     appkey
     * @return true ，拥有；false，不拥有
     */
    @GetMapping(value = "/{id}/permission")
    public boolean hasPermission(@PathVariable(value = "id") String itemBankId, @RequestParam("appId") String appKey) {
        return service.hasPermission(itemBankId, appKey);
    }

    /**
     * 根据题库id获取可用题库信息.
     *
     * @param itemBankId 题库id
     * @return 可用题库信息 course item bank info
     */
    @GetMapping(value = "/{id}")
    public CourseItemBank getCourseItemBankInfo(@PathVariable(value = "id") String itemBankId) {
        return service.getCourseItemBankInfo(itemBankId);
    }

    /**
     * 新增课程题库.
     *
     * @param model    课程题库信息
     * @param isNative 来源：True，内部；False，外部
     * @return the int
     */
    @PostMapping()
    public ReturnModel insert(@RequestBody ItemBank model, @RequestParam("isNative") boolean isNative) {
        return service.insert(model, isNative);
    }

    /**
     * 修改课程题库.
     *
     * @param id         题库id
     * @param name       题库课程名称
     * @return the int
     */
    @PutMapping(value = "/{id}")
    public int update(@PathVariable(value = "id") String id, @RequestParam("name") String name) {
        return service.update(id, name);
    }

    /**
     * 更新题库状态.
     *
     * @param id 题库ID
     * @return the int
     */
    @PutMapping(value = "/{id}/status")
    public int changeStatus(@PathVariable(value = "id") String id) {
        return service.changeStatus(id);
    }

    /**
     * 删除课程题库(外部不允许通过此接口删除).
     *
     * @param itemBankId 题库id
     * @param appKey     appKey
     * @return the int
     */
    @DeleteMapping(value = "/{id}")
    public int delete(@PathVariable(value = "id") String itemBankId, @RequestParam("appkey") String appKey) {
        return service.delete(itemBankId, appKey);
    }

    /**
     * 根据题库ID获取题型列表(admin).
     *
     * @param itemBankId 题库id
     * @param name       题型名称模糊查询
     * @return the list by item bank id and name
     */
    @GetMapping(value = "/{id}/questiontypelist")
    public List<QuestionTypeManageDto> getQuestionTypeListByItemBankIdAndName(@PathVariable(value = "id") String itemBankId,
                                                                              @RequestParam(value = "name") String name) {
        return questionTypeService.getListByItemBankIdAndName(itemBankId, name);
    }

    /**
     * 根据题库ID获取题型列表.
     *
     * @param itemBankId 题库id
     * @return 题型列表 question type list by item bank id
     */
    @GetMapping(value = "/{id}/questiontypes")
    public List<QuestionType> getQuestionTypeListByItemBankId(@PathVariable(value = "id") String itemBankId) {
        return questionTypeService.getListByItemBankId(itemBankId);
    }

    /**
     * 新增题型.
     *
     * @param itemBankId 所属题库id
     * @param model      题型传输模型
     * @return the return model
     */
    @PostMapping(value = "/{id}/questiontype")
    public ReturnModel insertQuestionType(@PathVariable(value = "id") String itemBankId, @RequestBody QuestionTypeDto model) {
        return questionTypeService.insert(itemBankId, model);
    }

    /**
     * 修改题型
     *
     * @param id         题型
     * @param itemBankId 所属题库id
     * @param model      the model
     * @return the int
     */
    @PutMapping(value = "/{id}/questiontype/{questionTypeId}")
    public int deleteQuestionType(@PathVariable(value = "questionTypeId") String id, @PathVariable(value = "id") String itemBankId, @RequestBody QuestionTypeDto model) {
        return questionTypeService.update(id, itemBankId, model);
    }

    /**
     * 更新题型状态.
     *
     * @param id         题型
     * @param itemBankId 所属题库id
     * @return the int
     */
    @PutMapping(value = "/{id}/questiontype/{questionTypeId}/status")
    public int changeQuestionTypeStatus(@PathVariable(value = "questionTypeId") String id, @PathVariable(value = "id") String itemBankId) {
        return questionTypeService.changeStatus(id, itemBankId);
    }

    /**
     * 删除题型.
     *
     * @param id         题型ID
     * @param itemBankId 所属题库ID
     * @return the int
     */
    @DeleteMapping(value = "/{id}/questiontype/{questionTypeId}")
    public int deleteQuestionType(@PathVariable(value = "questionTypeId") String id, @PathVariable(value = "id") String itemBankId) {
        return questionTypeService.delete(id, itemBankId);
    }

    /**
     * 恢复标记删除的修改题型使用关系状态.
     *
     * @param id         题型ID
     * @param itemBankId 所属题库ID
     * @return the int
     */
    @DeleteMapping(value = "/{id}/questiontype/{questionTypeId}/rollback")
    public int deleteRollbackQuestionType(@PathVariable(value = "questionTypeId") String id, @PathVariable(value = "id") String itemBankId) {
        return questionTypeService.deleteRollback(id, itemBankId);
    }
}
